export default {
    install: (app: { directive: (arg0: string, arg1: { mounted(el: any, binding: any): void; beforeUnmount(el: any, binding: any): void; }) => void; }) => {
        app.directive('clickoutside', {
            mounted(el, { value }) {
                const documentHandler = (e: Event) => {
                    if (el.contains(e.target as Node)) {
                        return;
                    }

                    if (typeof value === 'function') {
                        value(e);
                    }
                };

                el.__vueClickOutside__ = documentHandler; // 使用 `__vueClickOutside__` 作为私有变量名
                document.addEventListener('click', documentHandler);

            },
            beforeUnmount(el) {
                const documentHandler = el.__vueClickOutside__; // 获取之前绑定的事件处理器

                if (documentHandler) {
                    document.removeEventListener('click', documentHandler);
                    delete el.__vueClickOutside__;
                }
            },
        })
    }
}